#! /bin/sh -vx
# $Id$
# Copyright 2022-2023 TANAKA Takuji <ttk@t-lab.opal.ne.jp>
# You may freely use, modify and/or distribute this file.

BinDir=${BinDir:-.}
ExeExt=${ExeExt:-}
_bibtexu=$BinDir/bibtexu$ExeExt

test -d tests || mkdir -p tests

LC_ALL=C; export LC_ALL; LANGUAGE=C; export LANGUAGE
#
TEXMFCNF=$srcdir/../kpathsea; export TEXMFCNF
BSTINPUTS=$srcdir/tests; export BSTINPUTS
BIBINPUTS=$srcdir/tests; export BIBINPUTS


rc=0

## test for is.knj.str$

icuver=1000
$_bibtexu --version | sed -n 's/.*ICU version \([1-9][0-9]\)\..*/\1/p' \
  >tests/icuver.txt
icuver=`cat tests/icuver.txt`
if test -z "$icuver"; then
  echo "$0: quitting, could not determine icu version from bibtexu" >&2
  echo "$0: ($_bibtexu --version)" >&2
  $_bibtexu --version >&2
  exit 1
fi

echo "*** icuver="$icuver
if [ $icuver -lt 74 ]; then
  # skip CJK Ideograph Extension I
  # ex. U+2EBF0
  uchr='\x{2EBF0}'
fi
if [ $icuver -lt 72 ]; then
  # skip CJK Ideograph Extension H
  # ex. U+31350
  uchr=${uchr}'|\x{31350}'
fi
if [ $icuver -lt 70 ]; then
  # skip Kana Extended B
  # ex. U+1AFF0
  uchr=${uchr}'|\x{1AFF0}'
fi
if [ $icuver -lt 66 ]; then
  # skip CJK Ideograph Extension G, Small Kana Extension
  # ex. U+30000, U+1B150
  uchr=${uchr}'|\x{30000}|\x{1B150}'
fi
cp $srcdir/tests/iscjku.aux tests/xiscjku.aux
$_bibtexu tests/xiscjku || rc=1
cp $srcdir/tests/iscjku.bbl tests/oiscjku.bbl
if [ $icuver -le 73 ]; then
  echo "*** skip checking CJK Ideograph Extension G, H or I"
  perl -i".bak" -CD -ne "print unless /${uchr}/" tests/oiscjku.bbl
  perl -i".bak" -CD -ne "print unless /${uchr}/" tests/xiscjku.bbl
fi
diff tests/oiscjku.bbl tests/xiscjku.bbl || rc=2

exit $rc
